package com.YCH.common.rabbitMQ;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @Author: 辉哥
 * @DateTime: 2020/11/9 下午5:17
 * @Description: TODO
 */
@Configuration
@ConfigurationProperties(prefix = "spring.rabbitmq")
@PropertySource("classpath:rabbitmq.properties")
public class RabbitmqConfig {

    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";

    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";

    public static final String EXCHANGE_TOPIC_INFORM = "exchange_topic_inform";

    public static final String ROUTINGKEY_EMAIL = "inform.email.#";

    public static final String ROUTINGKEY_SMS = "inform.sms.#";

    // 声明交换机
    @Bean(EXCHANGE_TOPIC_INFORM)
    public Exchange EXCHANGE_TOPIC_INFORM(){
        // durable(true) 持久化, mq重启之后交换机还在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPIC_INFORM).durable(true).build();
    }

    // 声明QUEUE_INFORM_EMAIL队列
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue QUEUE_INFORM_EMAIL(){
        return new Queue(QUEUE_INFORM_EMAIL);
    }

    // 声明QUEUE_INFORM_SMS队列
    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SMS(){
        return new Queue(QUEUE_INFORM_SMS);
    }

    // ROUTINGKEY_EMAIL队列绑定交换机, 指定routingKey
    // @Qualifier注解给类成员变量使用时需要依赖@Autowired, 但是给方法参数可以单独使用
    @Bean
    public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue, @Qualifier (EXCHANGE_TOPIC_INFORM) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
    }

    // ROUTINGKEY_SMS队列绑定交换机, 指定routingKey
    // @Qualifier注解给类成员变量使用时需要依赖@Autowired, 但是给方法参数可以单独使用
    @Bean
    public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue, @Qualifier (EXCHANGE_TOPIC_INFORM) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
    }
}
